<?php

namespace application\api\controller;

use ticky\controller;

class index extends controller {

    public function valid_form() {
        $param = trim($_GET['param']); // 元素的值
        $name = trim($_GET['name']); // 元素的名称
        if (empty($param)) {
            $ret['info'] = "请输入.";
            $ret['status'] = 'n';
        } else {
            if ($name == 'username') {
                $result = db('member')->where(array('username' => $param))->find();
                if ($result) {
                    $ret['info'] = "该用户名已被占用，请更换其他用户名。";
                    $ret['status'] = 'n';
                } else {
                    $ret['info'] = "通过信息验证！";
                    $ret['status'] = 'y';
                }
            } elseif ($name == 'email') {
                $result = db('member')->where(array('email' => $param))->find();
                if ($result) {
                    $ret['info'] = "该邮箱已被占用，请更换其他邮箱。";
                    $ret['status'] = 'n';
                } else {
                    $ret['info'] = "通过信息验证！";
                    $ret['status'] = 'y';
                }
            } elseif ($name == 'email_verifycode') {
                $code = session("code_email");
                if ($code != $param) {
                    $ret['info'] = "邮箱验证码错误，请重新激活邮箱。";
                    $ret['status'] = 'n';
                } else {
                    $ret['info'] = "通过信息验证！";
                    $ret['status'] = 'y';
                }
            } elseif ($name == 'phone') {
                $result = db('member')->where(array('phone' => $param))->find();
                if ($result) {
                    $ret['info'] = "该手机号已被占用，请更换其他手机手机号码。";
                    $ret['status'] = 'n';
                } else {
                    $ret['info'] = "通过信息验证！";
                    $ret['status'] = 'y';
                }
            }
        }
        exit(json_encode($ret));
    }

    // api/index/sendeamil/to=271391233@qq.com&type=reg
    public function sendeamil() {
        $to = $_GET['to'];
        $subject = $_GET['subject'];
        $content = $_GET['content'];
        $type = $_GET['type'];
        $url = SITE_URL;
        $webname = get_config('webname');
        $adminemail = get_config('email');
        if ($type = 'reg') {
            $subject = "邮件验证码-会员注册";
            $code = rand(100000, 999999);
            session("code_email", $code);
            $content = " 亲爱的 {$to}，感谢您在我站注册了新帐号.<br/>你的验证码是{$code} 请您在30分钟内完成注册.<br/>请遵守当地法律法规。如果您有什么疑问可以联系管理员，Email: {$adminemail}。";
        }

//******************** 配置信息 ********************************
        $mailcontent = $content . <<<EOT
<br/><br/><br/>这封信是由<a href="{$url}" target= _blank>{$webname}</a> 发送的。<br />
您收到这封邮件，是由于在<a href="{$url}" target= _blank>{$webname}</a>使用了这个邮箱地址。<br />
如果您并没有访问过 <a href="{$url}" target= _blank>{$webname}</a>，或没有进行上述操作，请忽略这封邮件。<br />
您不需要退订或进行其他进一步的操作。<br />
EOT;
//************************ 配置信息 ****************************

        if (sendmail($to, $subject, $mailcontent)) {
            exit('发送邮件成功！');
        } else {
            exit('发送邮件失败！');
        }
    }

    /** 获取用户头像 */
    public function avatar() {
        $uid = isset($_GET['uid']) ? $_GET['uid'] : 0;
        $size = isset($_GET['size']) ? $_GET['size'] : '';
        $random = isset($_GET['random']) ? $_GET['random'] : '';
        $type = isset($_GET['type']) ? $_GET['type'] : '';
        $check = isset($_GET['check_file_exists']) ? $_GET['check_file_exists'] : '';
        $avatar = 'runtime/avatar/' . get_avatar($uid, $size, $type);
        //debug(ROOT_PATH . $avatar);
        if (file_exists(ROOT_PATH . $avatar)) {
            if ($check) {
                exit(1);
            }
            $random = !empty($random) ? rand(1000, 9999) : '';
            $avatar_url = empty($random) ? $avatar : $avatar . '?random=' . $random;
        } else {
            if ($check) {
                exit(0);
            }
            $size = in_array($size, array('big', 'middle', 'small')) ? $size : 'middle';
            $avatar_url = 'static/common/img/noavatar_' . $size . '.gif';
        }
        if (empty($random)) {
            header("HTTP/1.1 301 Moved Permanently");
            header("Last-Modified:" . date('r'));
            header("Expires: " . date('r', time() + 86400));
        }
        header('Location: ' . SITE_URL . '/' . $avatar_url);
        exit;
    }

}
